package com.gh.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.gh.yygh.hosp.service.ScheduleService;
import com.gh.yygh.model.hosp.Schedule;
import com.gh.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import com.gh.yygh.common.exception.YyghException;
import com.gh.yygh.common.helper.HttpRequestHelper;
import com.gh.yygh.common.result.Result;
import com.gh.yygh.common.result.ResultCodeEnum;
import com.gh.yygh.hosp.service.DepartmentService;
import com.gh.yygh.hosp.service.HospitalService;
import com.gh.yygh.hosp.service.HospitalSetService;
import com.gh.yygh.model.hosp.Department;
import com.gh.yygh.model.hosp.Hospital;
import com.gh.yygh.vo.hosp.DepartmentQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;

    //删除排班
    @ApiOperation(value = "查询排班接口")
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String hosScheduleId = (String)paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();
    }

    //查询排班接口
    @ApiOperation(value = "查询排班接口")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hoscode = (String)paramMap.get("hoscode");
        //科室编号
        String depcode = (String)paramMap.get("depcode");
        //当前页和每页数据
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page,limit,scheduleQueryVo);
        return Result.ok(pageModel);
    }

    //上传排班接口
    @ApiOperation(value = "上传排班接口")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    //删除科室接口
    @ApiOperation(value = "删除科室接口")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        //医院编号和科室编号
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        //签名校验

        departmentService.remove(hoscode,depcode);
        return Result.ok();

    }

    //查询科室接口
    @ApiOperation(value = "查询科室接口")
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String)paramMap.get("hoscode");
        //String depcode = (String)paramMap.get("depcode");
        //当前页和每页数据
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        //if(StringUtils.isEmpty(hoscode)) {
        //    throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        //}
        ////签名校验
        //if(!HttpRequestHelper.isSignEquals(paramMap, hospitalSetService.getSignKey(hoscode))) {
        //    throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        //}

        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //departmentQueryVo.setDepcode(depcode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }


    //上传科室接口
    @ApiOperation(value = "上传科室接口")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院传过来的签名，签名进行了md5加密
        String sign = (String)paramMap.get("sign");
        System.out.println("医院的sign是：：：" + sign);

        //根据传过来的医院编码查询数据库
        String hoscode = (String)paramMap.get("hoscode");
        String sinKey = hospitalSetService.getSignKey(hoscode);

        //校验签名是否相同
        System.out.println("数据库的sinKey是::" + sinKey);
        if(!sign.equals(sinKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        //调用service接口
        departmentService.save(paramMap);
        return Result.ok();
    }


    //查询医院
    @ApiOperation(value = "查询医院")
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院传过来的签名，签名进行了md5加密
        String sign = (String)paramMap.get("sign");
        System.out.println("医院的sign是：：：" + sign);

        //根据传过来的医院编码查询数据库
        String hoscode = (String)paramMap.get("hoscode");
        String sinKey = hospitalSetService.getSignKey(hoscode);

        //校验签名是否相同
        System.out.println("数据库的sinKey是::" + sinKey);
        if(!sign.equals(sinKey)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        //调用service实现
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok((hospital));
    };

    //上传医院接口
    @ApiOperation(value = "上传医院接口")
    @PostMapping("saveHospital")
    public Result saveHospital(HttpServletRequest request){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //获取医院传过来的签名，签名进行了md5加密
        String sign = (String)paramMap.get("sign");
        System.out.println("医院的sign是：：：" + sign);

        //根据传过来的医院编码查询数据库
        String hoscode = (String)paramMap.get("hoscode");
        String sinKey = hospitalSetService.getSignKey(hoscode);

        //校验签名是否相同
        System.out.println("数据库的sinKey是::" + sinKey);
        if(sign.equals(sinKey)){
            //传输过程中“+”变成了“ ”空格，我们要转换回来
            String logoData = (String)paramMap.get("logoData");
            logoData = logoData.replaceAll(" ", "+");
            paramMap.put("logoData",logoData);

            //调用service方法
            hospitalService.save(paramMap);
            return Result.ok();
        }else {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

    }
}
